package com.elitecrm.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	public static String getNowDateStr(){
		Date now = new Date();
		return formatDate(now);
	}
	
	public static String getNowDateStr(String pattern){
		Date now = new Date();
		return formatDate(now, pattern);
	}
	
	public static String formatDate(Date date){
		return formatDate(date,"yyyy-MM-dd HH:mm:ss");
	}
	
	public static String formatDate(Timestamp time){
		return formatDate(time,"yyyy-MM-dd HH:mm:ss");
	}
	
	public static String formatDate(Object obj,String formatPattern){
		if(obj!=null){
			SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
			return sdf.format(obj);
		}
		return "";
	}
	
	public static Timestamp parseStrToTimestamp(String dateStr,String formatPattern){
		if(dateStr != null){
			SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
			try {
				Date d = sdf.parse(dateStr);
				return new Timestamp(d.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static Timestamp parseStrToTimestamp(String dateStr){
		return parseStrToTimestamp(dateStr,"yyyy-MM-dd HH:mm:ss");
	}
}
